<?php
require_once 'Utils.php';
require_once 'OAuthResponse.php';
require_once 'OAuthServerBase.php';


/**
 * Demo REST server expose public method
 *
 * @author: Phong Linh <langxangvn@gmail.com>
*/
class UserServer extends OAuthServerBase {
	
	/**
	 * The data store
	 *
	 * @var OAuthStore 
	 *
	 */
	protected $_forceBypassNonce = true;	
	
	/**
	 * This is variable  description
	 *
	 */
	public static $ERR_USER_NOT_EXIST = 2001;
	
	/**
	 * Override get error
	 *
	 * @param mixed $code This is a description
	 * @return mixed This is the return value description
	 *
	 */
	public function getError($code) {
		try {
			return parent::getError($code);
		} catch(Exception $e) {
			if(self::$ERR_USER_NOT_EXIST == (int)$code) {
				return array('name' => 'User ID', 
					'msg' => 'User not exists.');
			}
			
			throw new Exception('Not defined error.');
		}
	}
	
	/**
	 * This is method handle
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function handle($action, $params = array()) {	
		switch ($action) {
			case 'profile':
				$result = $this->profile($params);
				break;							
			
			default:
				$code = self::$UNSUPPORTED_METHOD;
				$error = $this->getErrorMessage($code);
				throw new OAuthException($error, $code);
		}
		
		return $result;
	}
	
	/**
	 * This is method add
	 *
	 * @return mixed This is the return value description
	 *
	 */
	public function profile($params = array()) {		
		$this->verify('access', $this->_forceBypassNonce);
		
		$userId = $params['user_id'];
		$users = new Model_DbTable_Users();
		try {
			$user = $users->getUser($userId);
			unset($user['password']);
			unset($user['status']);
			return $user;
		} catch(Exception $e) {
			$code = self::$ERR_USER_NOT_EXIST;
			$error = $this->getErrorMessage($code);
			throw new OAuthException($error, $code);
		}
	}	
}

?>